package day6;
import day5.TreeNode;

public class ch1 {

    public static TreeNode n;
    public static TreeNode m;

    public static TreeNode k;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        n = p;
        m = q;
        fun(root);
        return k;


    }

    public int[] fun(TreeNode node){
        if(node==null) return new int[]{0,0};

        int[] l = fun(node.left);
        int[] r = fun(node.right);

        int[] arr = new int[]{0,0};
        if(node==n) arr[0]=1;
        if(node==m) arr[1]=1;

        arr[0] = arr[0]+l[0]+r[0];
        arr[1] = arr[1]+l[1]+r[1];

        if(arr[0]==1&&arr[1]==1){
            k = node;
            return new int[]{0,0};
        }else {
            return arr;
        }

    }




}
